﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BanDTBLL;
using PagedList;
namespace _1061102_1061114_1061129_1061132_1061141.Controllers
{ 
    public class TimKiemController : Controller
    {
        private ban_dien_thoai_dbEntities db = new ban_dien_thoai_dbEntities();

        //
        // GET: /TimKiem/

        public ViewResult Index(int? page, string currentFilter, string SearchTen, string tugia, string dengia, string mayanh, string wifi, string bluetooth, string camung, string hedieuhanh, string danhba)
        {
            if (Request.HttpMethod == "GET" && currentFilter != null)
            {
                SearchTen = currentFilter;
            }
            else if (Request.HttpMethod == "GET" && SearchTen == null)
            {
                SearchTen = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = SearchTen;

            var ds = from dt in db.san_pham
                     select dt;          
            if (!string.IsNullOrEmpty(SearchTen))
            {
                ViewBag.CurrentFilter = SearchTen;
                ds = ds.Where(dt => dt.ten.Contains(SearchTen));               
                //return View(ds.ToList());
            }
            if (!string.IsNullOrEmpty(tugia))
            {

                if (tugia != "-1")
                {
                    decimal temp;
                    temp = decimal.Parse(tugia) * 1000000;
                    ViewBag.TuGia = tugia;
                    ds = ds.Where(dt => dt.donGia > temp || dt.donGia == temp);
                    //return View(ds.ToList());
                }
            }
            if (!string.IsNullOrEmpty(dengia))
            {
                if (dengia != "-1")
                {
                    decimal temp;
                    temp = decimal.Parse(tugia) * 1000000;
                    ViewBag.DenGia = dengia;
                    ds = ds.Where(dt => dt.donGia < temp || dt.donGia == temp);
                    //return View(ds.ToList());
                }
            }
            if (!string.IsNullOrEmpty(mayanh))
            {
                if (mayanh != "-1")
                {
                    ViewBag.MayAnh = mayanh;
                    ds = ds.Where(dt => dt.may_anh.Contains(mayanh));
                    //return View(ds.ToList());
                }
            }
            if (!string.IsNullOrEmpty(wifi))
            {
                if (wifi != "-1")
                {
                    ViewBag.WiFi = wifi;
                    ds = ds.Where(dt => dt.wifi.Contains(wifi));
                    //return View(ds.ToList());
                }
            }
            if (!string.IsNullOrEmpty(bluetooth))
            {
                if (bluetooth != "-1")
                {
                    ViewBag.Bluetooth = bluetooth;
                    ds = ds.Where(dt => dt.bluetooth.Contains(bluetooth));
                    //return View(ds.ToList());
                }
            }
            if (!string.IsNullOrEmpty(camung))
            {
                if (camung != "-1")
                {
                    ViewBag.CamUng = camung;
                    ds = ds.Where(dt => dt.cam_ung.Contains(camung));
                    //return View(ds.ToList());
                }
            }
            if (!string.IsNullOrEmpty(hedieuhanh))
            {
                if (hedieuhanh != "-1")
                {
                    ViewBag.HeDieuHanh = hedieuhanh;
                    ds = ds.Where(dt => dt.he_dieu_hanh.Contains(hedieuhanh));
                    //return View(ds.ToList());
                }
            }
            if (!string.IsNullOrEmpty(danhba))
            {
                if (danhba != "-1")
                {
                    ViewBag.DanhBa = danhba;
                    ds = ds.Where(dt => dt.danh_ba.Contains(danhba));
                    //return View(ds.ToList());
                }
            }
            ds = ds.OrderBy(dt => dt.ten);
            int pageSize = 3;
            int pageNumber = (page ?? 1);
            return View(ds.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /TimKiem/Details/5

        public ViewResult Details(int id)
        {
            san_pham san_pham = db.san_pham.Single(s => s.ma == id);
            return View(san_pham);
        }

        //
        // GET: /TimKiem/Create

        public ActionResult Create()
        {
            ViewBag.maLoaiSP = new SelectList(db.loai_san_pham, "ma", "tenLoai");
            ViewBag.trangThai = new SelectList(db.trang_thai_sp, "maTrangThai", "tenTrangThai");
            return View();
        } 

        //
        // POST: /TimKiem/Create

        [HttpPost]
        public ActionResult Create(san_pham san_pham)
        {
            if (ModelState.IsValid)
            {
                db.san_pham.AddObject(san_pham);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.maLoaiSP = new SelectList(db.loai_san_pham, "ma", "tenLoai", san_pham.maLoaiSP);
            ViewBag.trangThai = new SelectList(db.trang_thai_sp, "maTrangThai", "tenTrangThai", san_pham.trangThai);
            return View(san_pham);
        }
        
        //
        // GET: /TimKiem/Edit/5
 
        public ActionResult Edit(int id)
        {
            san_pham san_pham = db.san_pham.Single(s => s.ma == id);
            ViewBag.maLoaiSP = new SelectList(db.loai_san_pham, "ma", "tenLoai", san_pham.maLoaiSP);
            ViewBag.trangThai = new SelectList(db.trang_thai_sp, "maTrangThai", "tenTrangThai", san_pham.trangThai);
            return View(san_pham);
        }

        //
        // POST: /TimKiem/Edit/5

        [HttpPost]
        public ActionResult Edit(san_pham san_pham)
        {
            if (ModelState.IsValid)
            {
                db.san_pham.Attach(san_pham);
                db.ObjectStateManager.ChangeObjectState(san_pham, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.maLoaiSP = new SelectList(db.loai_san_pham, "ma", "tenLoai", san_pham.maLoaiSP);
            ViewBag.trangThai = new SelectList(db.trang_thai_sp, "maTrangThai", "tenTrangThai", san_pham.trangThai);
            return View(san_pham);
        }

        //
        // GET: /TimKiem/Delete/5
 
        public ActionResult Delete(int id)
        {
            san_pham san_pham = db.san_pham.Single(s => s.ma == id);
            return View(san_pham);
        }

        //
        // POST: /TimKiem/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            san_pham san_pham = db.san_pham.Single(s => s.ma == id);
            db.san_pham.DeleteObject(san_pham);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}